package com.huawei.appmarket.service.externalapi.actions;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.huawei.appgallery.assistantdock.base.externalaction.openviewaction.RemoteBuoyAction;
import com.huawei.appmarket.service.externalapi.jumpers.CommonActionJumper;
import com.huawei.appmarket.service.thirdappdl.ThirdAppDownloadActivityProtocol;
import com.huawei.appmarket.service.thirdappdl.ThirdAppDownloadActivityProtocol.Request;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import o.ceb;
import o.cjk;
import o.cjm;
import o.cjo;
import o.dgh;
import o.Logger;
import o.dyr;
import o.dys.a;
import o.dyw;
import o.erz;

public class ViewAction extends dyr {
    private static final String HOST_DETAILS = "details";
    private static final String SCHEME_MARKET = "market";
    private static final String TAG = "ViewAction";
    private Map<String, Class<? extends dyw>> VIEWACTION_JUMPERS = new HashMap();

    public ViewAction(a aVar) {
        super(aVar);
        registerViewActionJumpers();
    }

    protected void registerViewActionJumpers() {
        registerJumper(SCHEME_MARKET, HOST_DETAILS, CommonActionJumper.class);
    }

    protected void registerJumper(String str, String str2, String str3, Class<? extends dyw> cls) {
        this.VIEWACTION_JUMPERS.put(str + "://" + str2 + str3, cls);
    }

    protected void registerJumper(String str, String str2, Class<? extends dyw> cls) {
        this.VIEWACTION_JUMPERS.put(str + "#" + str2, cls);
    }

    private Class<? extends dyw> getActionJumperClass(String str, String str2) {
        return (Class) this.VIEWACTION_JUMPERS.get(str + "#" + str2);
    }

    private Class<? extends dyw> getActionJumperClass(String str) {
        for (Entry entry : this.VIEWACTION_JUMPERS.entrySet()) {
            if (str.startsWith((String) entry.getKey())) {
                return (Class) entry.getValue();
            }
        }
        return null;
    }

    private dyw getViewActionJumper(Uri uri, String str, String str2) {
        Class actionJumperClass = getActionJumperClass(str, str2);
        if (actionJumperClass == null) {
            actionJumperClass = getActionJumperClass(uri.toString());
            if (actionJumperClass == null) {
                return null;
            }
        }
        try {
            return (dyw) actionJumperClass.getConstructor(new Class[]{dyr.class, a.class, Uri.class}).newInstance(new Object[]{this, this.callback, uri});
        } catch (Throwable e) {
            Logger.ˋ(TAG, "init ViewActionJumper failed!", e);
            return null;
        } catch (Throwable e2) {
            Logger.ˋ(TAG, "init ViewActionJumper failed!", e2);
            return null;
        } catch (Throwable e22) {
            Logger.ˋ(TAG, "init ViewActionJumper failed!", e22);
            return null;
        } catch (Throwable e222) {
            Logger.ˋ(TAG, "init ViewActionJumper failed!", e222);
            return null;
        }
    }

    public boolean isNeedLoading() {
        return true;
    }

    public long getTimeout() {
        return 5000;
    }

    private void onShareAnalyticEvent(String str) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(RemoteBuoyAction.REMOTE_BUOY_URI, str);
        dgh.ˋ("330101", linkedHashMap);
    }

    public LinkedHashMap<String, String> getReportMap() {
        LinkedHashMap<String, String> reportMap = super.getReportMap();
        if (!(this.callback == null || this.callback.getIntent() == null)) {
            Uri data = this.callback.getIntent().getData();
            if (!(reportMap == null || data == null)) {
                try {
                    reportMap.put(dyr.BI_KEY_URL, URLEncoder.encode(data.toString(), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    Logger.ˎ(TAG, "UnsupportedEncodingException: encode url String failed.");
                }
            }
        }
        return reportMap;
    }

    public void onAction() {
        Uri data;
        Intent intent = this.callback.getIntent();
        if (intent != null) {
            data = intent.getData();
        } else {
            data = null;
        }
        if (data != null) {
            String uri = data.toString();
            try {
                uri = URLDecoder.decode(uri, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                Logger.ˋ(TAG, "error to decode uriString");
            }
            onShareAnalyticEvent(uri);
            uri = data.getScheme();
            String host = data.getHost();
            if (uri.isEmpty() || host.isEmpty()) {
                this.callback.finish();
                return;
            } else if (checkVersion(data)) {
                dyw viewActionJumper = getViewActionJumper(data, uri, host);
                if (viewActionJumper == null) {
                    this.callback.finish();
                    return;
                }
                Logger.ˏ(TAG, "actionJumperName = " + viewActionJumper.getClass().getSimpleName());
                viewActionJumper.ॱ();
                return;
            } else {
                this.callback.finish();
                return;
            }
        }
        this.callback.finish();
    }

    private boolean checkVersion(Uri uri) {
        boolean z;
        String ˏ = ceb.ˏ(uri, "V");
        if (TextUtils.isEmpty(ˏ)) {
            ˏ = ceb.ˏ(uri, "v");
        }
        if (TextUtils.isEmpty(ˏ)) {
            z = true;
        } else {
            try {
                int parseInt = Integer.parseInt(ˏ);
                int ˎ = erz.ˎ(this.callback.ˊ(), this.callback.ˊ().getPackageName());
                Logger.ˏ(TAG, "TargerVersion: " + ˏ + ", LocalVersion: " + ˎ);
                if (ˎ >= parseInt) {
                    z = true;
                } else {
                    handleLowerVersion(parseInt, ˎ, uri.toString());
                    z = false;
                }
            } catch (NumberFormatException e) {
                Logger.ˋ(TAG, "Format error! cannot parse versionCode to Integer");
                return false;
            }
        }
        return z;
    }

    private void handleLowerVersion(int i, int i2, String str) {
        Logger.ˎ(TAG, "target:" + i + ",current:" + i2 + " is lower");
        cjo thirdAppDownloadActivityProtocol = new ThirdAppDownloadActivityProtocol();
        Request request = new Request();
        request.ˊ(i2);
        request.ˋ(i);
        request.ˋ(str);
        thirdAppDownloadActivityProtocol.ˊ(request);
        cjk.ˎ().ˋ(this.callback.ˊ(), new cjm("show.update.activity", thirdAppDownloadActivityProtocol));
    }
}
